<template>
  <div class="employees">
    <el-card>
      <pageTools>
        <template #right>
          <div>
            <el-button type="primary" @click="goUpload">导入</el-button>
            <el-button type="primary" @click="addClick">+ 新增员工</el-button>
          </div>
        </template>
      </pageTools>
    </el-card>
    <el-card style="margin-top: 20px">
      <el-table :data="list">
        <el-table-column label="序号" width="50">
          <template v-slot="{ $index }">
            <div>
              {{ (page.page - 1) * page.size + $index + 1 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="姓名" prop="username" />
        <el-table-column label="员工头像" prop="staffPhoto">
          <!--
            1:创建一个dialog弹框
            2：弹框内要有一个canvas  ref='canvas'
            3:导入qrcode
            4:点击头像
              qrcode.toCanvas(canvasdom,头像地址,{宽高...})

           -->
          <template v-slot="{ row }">
            <img
              v-imgError="defaultImg"
              class="avatar"
              :src="row.staffPhoto"
              alt=""
              @click="imgClick(row.staffPhoto)"
            >
          </template>
        </el-table-column>
        <el-table-column label="手机号" prop="mobile" />
        <el-table-column label="工号" prop="workNumber" />

        <!-- <el-table-column
          label="聘用形势"
          prop="formOfEmployment"
          :formatter="formatter"
          width="80"
        >
        </el-table-column> -->
        <!-- 过滤器 -->
        <!-- <el-table-column label="聘用形势" prop="formOfEmployment" width="80">
          <template v-slot="{ row }">
            <div>
              {{ row.formOfEmployment | formatterFormOf }}
            </div>
          </template>
        </el-table-column> -->
        <!-- 自定义指令 -->
        <!-- <el-table-column label="聘用形势" prop="formOfEmployment" width="80">
          <template v-slot="{ row }">
            <div v-formatterFormOf="row.formOfEmployment" />
          </template>
        </el-table-column> -->
        <el-table-column label="聘用形势" prop="formOfEmployment" width="80" />
        <el-table-column label="部门" prop="departmentName" />
        <el-table-column
          label="入职日期"
          prop="timeOfEntry"
          :formatter="formatterTimeOfEntry"
        />
        <el-table-column label="操作" width="300" fixed="right">
          <template v-slot="{ row }">
            <div>
              <el-button type="text" @click="look(row)">查看</el-button>
              <el-button type="text">转正</el-button>
              <el-button type="text">调岗</el-button>
              <el-button type="text">离职</el-button>
              <!--  -->
              <el-button type="text" @click="roleClick(row)">角色</el-button>
              <el-button type="text" @click="del(row)">删除</el-button>
              <!-- 删除
              1：确定弹框
              2：点击了确定接口调用（定义与导入api）
                 调用成功
                   提示
                   刷新列表：注意点：最好回到第一页
               -->
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <el-pagination
          :current-page="page.page"
          :page-sizes="[1, 3, 10, 100, 200, 300, 400]"
          :page-size="page.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <!-- 新增组件 -->
    <Add ref="add" @getData="getData" />
    <!-- 头像二维码弹框 -->
    <el-dialog title="二维码" :visible.sync="showQrcode">
      <div style="text-align: center">
        <canvas ref="canvas" />
      </div>
    </el-dialog>
    <!-- 分配角色 -->
    <Role ref="role" v-model="showRole" />
  </div>
</template>
<script>
import { sysUser, sysUserDelete } from '@/api/employees.js'
import employeesData from '@/api/constant/employees.js'
import Add from './components/add.vue'
import dayjs from 'dayjs'
import Qrcode from 'qrcode'
import Role from './components/role.vue'
export default {
  components: {
    Add,
    Role
  },
  data() {
    return {
      showRole: false,
      showQrcode: false,
      defaultImg: require('@/assets/common/head.jpg'),
      list: [],
      page: {
        page: 1, //	是	1	页码
        size: 10, //	是	10	每页条数
        total: 100
      }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    handleSizeChange(size) {
      this.page.size = size
      this.page.page = 1
      this.getData()
    },
    handleCurrentChange(page) {
      this.page.page = page
      this.getData()
    },
    // 获取列表
    async getData() {
      const res = await sysUser(this.page)
      this.page.total = res.data.total
      // 格式化聘用形势
      res.data.rows.forEach((item) => {
        item.formOfEmployment = this.formatter(
          null,
          null,
          item.formOfEmployment
        )
      })
      this.list = res.data.rows
      console.log('员工列表', res)
    },
    // 格式化聘用形势
    formatter(row, column, cellValue, index) {
      // array.find(查找 满足条件的项，找到了就返回找到项数据，找不到返回undefined)
      const temp = employeesData.hireType.find((item) => {
        return item.id === +cellValue
      })
      return temp ? temp.value : '-'
    },
    // 数据转换
    formatterTimeOfEntry(row, column, cellValue, index) {
      // console.log(cellValue)
      /*
      dayjs
       1:下载  npm i dayjs
       2:导入  import dayjs from 'dayjs'
       3:使用
          dayjs(时间数据).format('YYYY-MM-DD HH(hh):mm:ss')
      */
      return dayjs(cellValue).format('YYYY-MM-DD')
    },
    // 新增点击事件
    addClick() {
      // 打开新增组件弹框
      this.$refs.add.dialogVisible = true
    },
    // 删除
    del(row) {
      this.$confirm('你确定删除该员工吗？', '提示').then(async() => {
        await sysUserDelete(row.id)
        this.$message.success('删除成功')
        this.handleCurrentChange(1)
      })
    },
    // 导入点击
    goUpload() {
      this.$router.push('/employees/upload?name=' + encodeURIComponent('员工'))
    },
    // 查看跳转
    look(row) {
      // console.log(row)
      this.$router.push('/employees/detail/' + row.id)
    },
    // 头像点击
    imgClick(imgSrc) {
      // 如果没有图片地址就不理
      if (!imgSrc) {
        return
      }
      // 显示弹框
      this.showQrcode = true
      this.$nextTick(() => {
        // 生成二维码
        Qrcode.toCanvas(this.$refs.canvas, imgSrc, { width: 400, height: 400 })
      })
    },
    // role按钮点击
    roleClick(row) {
      this.showRole = true
      this.$refs.role.getUserInfo(row.id)
    }
  }
}
</script>
<style lang="scss" scoped>
.employees {
  padding: 15px;
  .page {
    margin-top: 20px;
    text-align: right;
  }
  .avatar {
    width: 30px;
    height: 30px;
    object-fit: contain;
  }
}
</style>
